<template>
  <div
    class="modal fade"
    tabindex="-1"
    role="dialog"
    aria-hidden="true"
    :id="id"
  >
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title">
            <slot name="title"> 调整现金账户 </slot>
          </h4>
          <button
            type="button"
            class="btn-close"
            data-bs-dismiss="modal"
            aria-label="Close"
          ></button>
        </div>
        <div class="modal-body">
          <form>
            <div class="row mb-2">
              <label class="control-label col-md-4"> 授权给 </label>
                <div class="col-md-6">
                  <select class="form-select" v-model.number="authorizee">
                    <option
                      v-for="info in dataList"
                      v-bind:value="info.enterpriseId"
                      :key="info.id"
                    >
                      {{ info.name }}
                    </option>
                  </select>
                </div>
            </div>
            <div class="row mb-2">
               <label class="control-label col-md-4"> +/- </label>
                <div class="col-md-6">
                  <input
                    type="radio"
                    name="opMode"
                    value="0"
                    v-model.number="opMode"
                  />扣款
                  <input
                    type="radio"
                    name="opMode"
                    value="1"
                    v-model.number="opMode"
                  />充值
                </div>
            </div>
            <div class="row mb-2">
                <label class="control-label col-md-4"> 调整金额 </label>
                <div class="col-md-6">
                  <input
                    type="text"
                    class="form-control"
                    v-model.number="amount"
                  />
                  <span class="help-text">整数，单位元</span>
                </div>
              </div>

              <div class="row">
                <label class="control-label col-md-4"> 备注(调整事由) </label>
                <div class="col-md-6">
                  <input
                    type="text"
                    class="form-control"
                    v-model.trim="remark"
                  />
                </div>
              </div>
          </form>
        </div>
        <div class="modal-footer">
          <button
            type="button"
            class="btn btn-secondary me-2"
            data-bs-dismiss="modal"
          >
            关闭
          </button>
          <button type="button" class="btn btn-primary" @click.stop="ok()">
            确定
          </button>
        </div>
      </div>
    </div>
  </div>
</template>


<script>
import $ from "jquery";

export default {
  props: {
    nullable: {
      type: Boolean,
      default: true,
    },
    id: {
      type: String,
      default: "customerCashBalanceModal",
    },
    dataList: {
      type: Array
    }
  },
  data() {
    return {
      resolve: "",
      reject: "",
      promise: "",

      authorizee: 0,
      opMode: 0,
      amount: 0,
      remark: "",

      myModal: null,
    };
  },
  methods: {
    ok: function () {
      this.hideModal();


      this.resolve({
        authorizee: this.authorizee,
        opMode: this.opMode,
        amount: this.amount,
        remark: this.remark,
      });
    },
    modal: function (authorizee) {
      this.authorizee = authorizee;

      const self = this;
      const myModalEl = $(this.$el).get(0);
      myModalEl.addEventListener("shown.bs.modal", function () {
        console.log("shown.bs.modal");
        self.canShowSelect2 = 1;
      });

      this.showModal();

      this.promise = new Promise((resolve, reject) => {
        this.resolve = resolve;
        this.reject = reject;
      });
      return this.promise;
    },
    showModal: function () {
      this.getMyModal().show();
    },
    hideModal: function () {
      this.getMyModal().hide();
    },
    getMyModal: function () {
      if (this.myModal === null) {
        let myModalEl = $(this.$el).get(0);
        this.myModal = new bootstrap.Modal(myModalEl, {
          keyboard: true,
        });
      }

      return this.myModal;
    },
  },
};
</script>
